﻿// This source file is part of WallpaperControl 0.4, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more info.
//
// Copyright (c) 2009 Niklas Kroll
// Original code copyright (c) Sean Campbell
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// This code is taken from Sean Campbells blog article about setting the windows 
// wallpaper on Coding4Fun. 
// Link: http://blogs.msdn.com/coding4fun/archive/2006/10/31/912569.aspx

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using Microsoft.Win32;

namespace wpControl1
{
    public class Wallpaper
    {
        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        public Wallpaper()
        {
            
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(
            int uAction, int uParam, string lpvParam, int fuWinIni);

        /// <summary>
        /// The styles how a wallpaper can be set if it doesn't fit the screen.
        /// </summary>
        public enum Style : int
        {
            /// <summary>
            /// The selected image is stretched until it fits the screen.
            /// </summary>
            Stretched,
            /// <summary>
            /// The image is displayed multiple times until the screen is filled.
            /// </summary>
            Tiled, 
            /// <summary>
            /// The single image is displayed. It is positioned in the center of the screen.
            /// </summary>
            Centered
        }

        

        /// <summary>
        /// Sets the windows wallpaper.
        /// </summary>
        /// <param name="path">The path to the image file.</param>
        /// <param name="style">The syle how the wallpaper should be displayed if it doesn't fit the screen.</param>
        /// <exception cref="System.IO.FileNotFoundException">Thrown if the specified file is not existing.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the param 'path' is null or an empty string.</exception>
        public void SetWallpaper(string path, Style style)
        {
            if (path == null || path == string.Empty)
            {
                throw new ArgumentNullException();
            }

            if (File.Exists(path))
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);

                switch (style)
                {
                    case Style.Stretched:
                        key.SetValue(@"WallpaperStyle", "2");
                        key.SetValue(@"TileWallpaper", "0");
                        break;
                    case Style.Centered:
                        key.SetValue(@"WallpaperStyle", "1");
                        key.SetValue(@"TileWallpaper", "0");
                        break;
                    case Style.Tiled:
                        key.SetValue(@"WallpaperStyle", "1");
                        key.SetValue(@"TileWallpaper", "1");
                        break;
                }
                SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
    }
}
